package com.wuyou.simple;

/**
 * 2379. 得到 K 个黑块的最少涂色次数
 *
 * @Date 2024/12/4 15:19
 * @Author 无忧
 */
public class MinimumRecolors {

    public static void main(String[] args) {
        String blocks = "WBWBBBW";
        int k = 2;
        System.out.println(sol_1(blocks, k));
    }

    public static int sol_1(String blocks, int k) {
        int l = 0, r = 0, res = 0, b_count = 0, w_count = 0;
        char[] charArray = blocks.toCharArray();
        while (r < charArray.length) {
            if(r < k){
                if (charArray[r++] == 'W') {
                    w_count++;
                } else {
                    b_count++;
                }
                res = k - b_count;
            }else {
                if (charArray[r++] == 'W') {
                    if (charArray[l++] == 'B') {
                        b_count--;
                        w_count++;
                    }
                } else {
                    if (charArray[l++] == 'W') {
                        b_count++;
                        w_count--;
                    }
                }
                res = Math.min(res, w_count);
            }
        }
        return res;
    }
}
